﻿<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:dyn="http://exslt.org/dynamic"
    xmlns:model="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable"
    exclude-result-prefixes="msxsl dyn">

  <xsl:output method="xml" indent="yes" />
  <msxsl:script implements-prefix="dyn" language="C#">
    <![CDATA[
public XPathNodeIterator SelectElements(XPathNavigator context, string guid, string id)
{
  XPathNodeIterator result = context.Select("//*[@guid = '" + guid + "' and @id='" + id + "']");
  return result;
}
public string GetTargetName(XPathNavigator context, string guid, string id)
{
  XPathNodeIterator result = context.Select("//*[@guid = '" + guid + "' and @id='" + id + "']");
  while(result.MoveNext())
  {
    if( result.Current.Name == "Menu")
      return "Menu";
    if( result.Current.Name == "Group")
      return "Group";
  }
  return "Extern";
}
public string GetCustomToolGenerator(XPathNavigator context)
{
  XPathNodeIterator result = context.Select("comment()");
  while(result.MoveNext())
  {
    string comment = result.Current.Value;
    int pos = comment.IndexOf('{')+1;
    int pos2 = comment.IndexOf('}');
    if( pos > 0 && pos2 > pos)
    {
        return comment.Substring(pos, pos2-pos);
    }
  }
  return String.Empty;
}
]]>
  </msxsl:script>

  <xsl:template match="model:Parent">
    <xsl:call-template name="EmitParent">
      <xsl:with-param name="guid" select="@guid"/>
      <xsl:with-param name="id" select="@id"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="EmitParent">
    <xsl:param name="guid"/>
    <xsl:param name="id"/>
    <xsl:variable name="targetName" select="dyn:GetTargetName(/, $guid, $id)"/>
    <xsl:choose>
      <xsl:when test="$targetName='Menu'">
        <xsl:element name="ParentMenu" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:attribute name="id">
            <xsl:value-of select="concat('/', $guid, '/', $id)"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$targetName='Group'">
        <xsl:element name="ParentGroup" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:attribute name="id">
            <xsl:value-of select="concat('/', $guid, '/', $id)"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="ParentExtern" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:attribute name="id">
            <xsl:value-of select="concat('/', $guid, '/', $id)"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="model:Commands">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"></xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="model:Button|model:Group|model:Menu|model:Combo">
    <xsl:copy>
      <xsl:apply-templates select="@*|model:Parent"></xsl:apply-templates>

      <xsl:for-each select="dyn:SelectElements(/, @guid, @id)">
        <xsl:if test="name() = 'CommandPlacement'">
          <xsl:call-template name="EmitParent">
            <xsl:with-param name="guid" select="model:Parent/@guid"/>
            <xsl:with-param name="id" select="model:Parent/@id"/>
          </xsl:call-template>
        </xsl:if>
        <xsl:if test="name() = 'KeyBinding'">
          <xsl:copy>
            <xsl:apply-templates select="@editor"/>
            <xsl:apply-templates select="@key1"/>
            <xsl:apply-templates select="@mod1"/>
            <xsl:apply-templates select="@key2"/>
            <xsl:apply-templates select="@mod2"/>
            <xsl:apply-templates select="@emulator"/>
          </xsl:copy>
        </xsl:if>
      </xsl:for-each>

      <xsl:apply-templates select="model:Icon"/>
        <xsl:apply-templates select="model:Strings"/>

      <xsl:for-each select="dyn:SelectElements(/, @guid, @id)">
        <xsl:if test="name() = 'VisibilityItem'">
          <xsl:element name="VisibilityContext" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
            <xsl:value-of select="@context"/>
          </xsl:element>
        </xsl:if>
      </xsl:for-each>

      <xsl:apply-templates select="model:CommandFlag"/>

    </xsl:copy>
  </xsl:template>

  <xsl:template match="model:Icon">
    <xsl:element name="Icon" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
      <xsl:element name="commandHasIcon" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
        <xsl:attribute name="iconId">
          <xsl:value-of select="@id"/>
        </xsl:attribute>
        <xsl:element name="bitmapMoniker" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
          <xsl:attribute name="guid">
            <xsl:value-of select="concat('/00000000-0000-0000-0000-000000000001/', @guid)"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template match="model:CommandTable">
    <xsl:copy>
      <xsl:attribute name="dslVersion">1.0.0.0</xsl:attribute>
      <xsl:attribute name="Id">00000000-0000-0000-0000-000000000000</xsl:attribute>
      <xsl:attribute name="language">
        <xsl:value-of select="@language"/>
      </xsl:attribute>
      <xsl:attribute name="customToolGenerator">
        <xsl:value-of select="dyn:GetCustomToolGenerator(.)"/>
      </xsl:attribute>
      <xsl:apply-templates select="child::node()"></xsl:apply-templates>
      <!-- Bitmaps doit exister. Si il n'existe pas, on en crée un vide -->
      <!-- Déplacement de Bitmaps de CommandTable dans Commands (pour pouvoir gérer les swimlanes) -->
      <xsl:element name="Bitmaps" namespace="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable">
        <xsl:attribute name="Id">00000000-0000-0000-0000-000000000001</xsl:attribute>
        <xsl:apply-templates select="model:Commands/model:Bitmaps/model:Bitmap"></xsl:apply-templates>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"></xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <!-- Ignore -->
  <xsl:template match="model:CommandPlacements"/>
  <xsl:template match="model:KeyBindings"/>
  <xsl:template match="model:VisibilityConstraints"/>
  <xsl:template match="model:Bitmaps"/>

</xsl:stylesheet>